package com.ssm.sport.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssm.sport.entity.Banner;
import com.ssm.sport.mapper.BannerMapper;
import com.ssm.sport.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 轮播图服务实现类
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {

    @Autowired
    private BannerMapper bannerMapper;

    @Override
    public List<Banner> listEnabledBanners() {
        return bannerMapper.selectEnabledList();
    }

    @Override
    public Banner getById(Integer id) {
        return bannerMapper.selectById(id);
    }

    @Override
    public boolean add(Banner banner) {
        if (banner.getStatus() == null) {
            banner.setStatus(1); // 默认启用
        }
        
        if (banner.getSortOrder() == null) {
            banner.setSortOrder(0); // 默认排序为0
        }
        
        Date now = new Date();
        banner.setCreateTime(now);
        banner.setUpdateTime(now);
        
        return bannerMapper.insert(banner) > 0;
    }

    @Override
    public boolean update(Banner banner) {
        banner.setUpdateTime(new Date());
        return bannerMapper.updateById(banner) > 0;
    }

    @Override
    public boolean delete(Integer id) {
        return bannerMapper.deleteById(id) > 0;
    }

    @Override
    public Page<Banner> list(int pageNum, int pageSize) {
        Page<Banner> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Banner> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort_order");
        
        bannerMapper.selectPage(page, queryWrapper);
        return page;
    }
    
    @Override
    public Page<Banner> getBannerPage(int pageNum, int pageSize) {
        return list(pageNum, pageSize);
    }
    
    @Override
    public Banner getBannerById(Integer id) {
        return getById(id);
    }
    
    @Override
    public boolean saveBanner(Banner banner) {
        return add(banner);
    }
    
    @Override
    public boolean updateBanner(Banner banner) {
        return update(banner);
    }
    
    @Override
    public boolean deleteBanner(Integer id) {
        return delete(id);
    }
    
    @Override
    public boolean updateBannerStatus(Integer id, Integer status) {
        if (id == null || status == null) {
            return false;
        }
        
        UpdateWrapper<Banner> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id)
                    .set("status", status)
                    .set("update_time", new Date());
        
        return bannerMapper.update(null, updateWrapper) > 0;
    }
} 